package com.zzedu.manager.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.zzedu.common.enums.CommonEnums;
import com.zzedu.common.utils.SecurityUtils;
import com.zzedu.common.utils.bean.BeanUtils;
import com.zzedu.device.req.GetUnitListByLevelPageList;
import com.zzedu.device.req.TeacherClassPageList;
import com.zzedu.device.rsp.ApiZZCourseInfoVO;
import com.zzedu.device.rsp.ApiZzClassesInfoVO;
import com.zzedu.domain.ZzClasses;
import com.zzedu.domain.ZzCourse;
import com.zzedu.domain.ZzTeacher;
import com.zzedu.dto.ZzClassesDTO;
import com.zzedu.manager.ITeacherClassManager;
import com.zzedu.result.Result;
import com.zzedu.result.ResultEnum;
import com.zzedu.service.*;
import com.zzedu.utils.PageData;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class TeacherClassManagerImpl implements ITeacherClassManager {

    @Autowired
    IZzClassesService zzClassesService;
    @Autowired
    IZzTeacherService zzTeacherService;

    @Autowired
    IZzZzStuClassesRService zzZzStuClassesRService;


    @Autowired
    IZzCourseService zzCourseService;


    @Autowired
    IZzSchoolService zzSchoolService;

    @Override
    public Result<PageData<ApiZzClassesInfoVO>> pageList(TeacherClassPageList data) {
        Result<PageData<ApiZzClassesInfoVO>> result = new Result<>();
        ZzTeacher zzTeacher = zzTeacherService.getById(SecurityUtils.getUserId());
        ZzClassesDTO zzClassesDTO = new ZzClassesDTO();
        zzClassesDTO.setSchoolId(zzTeacher.getSchoolId());
        zzClassesDTO.setTeacherId(zzTeacher.getId());
        zzClassesDTO.setState(CommonEnums.YES.getCode());
        IPage<ZzClasses> page = zzClassesService.page(Page.of(data.getPageNum(), data.getPageSize()), zzClassesService.buildQuery(zzClassesDTO));
        PageData<ApiZzClassesInfoVO> pageData = new PageData<>();
        pageData.setTotal(page.getTotal());
        pageData.setRowData(convertApiZzClassesInfoVO(page.getRecords()));
        result.setData(pageData);
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        return result;
    }

    @Override
    public Result<PageData<ApiZZCourseInfoVO>> getUnitListByLevel(GetUnitListByLevelPageList data) {
        // 从学校的课表中找出《=当前时间的unit
        ZzTeacher zzTeacher = zzTeacherService.getById(SecurityUtils.getUserId());
        IPage<ZzCourse> page = null;
        if (this.zzSchoolService.isTestSchoolFlag(zzTeacher.getSchoolId())) {
            page = zzCourseService.page(Page.of(data.getPageNum(), data.getPageSize()), Wrappers.lambdaQuery(ZzCourse.class)
                    .eq(ZzCourse::getLevel, data.getLevel())
                    .orderByAsc(ZzCourse::getSortNo)
                    .orderByAsc(ZzCourse::getId));
        } else {
            page = zzCourseService.page(Page.of(data.getPageNum(), data.getPageSize()), Wrappers.lambdaQuery(ZzCourse.class)
                    .eq(ZzCourse::getLevel, data.getLevel())
                    .eq(ZzCourse::getState, CommonEnums.YES.getCode())
                    .exists("select unit_id from zz_school_course where deleted=0 and school_id={0} and level={1} and unit_id=zz_course.id", zzTeacher.getSchoolId(), data.getLevel())
                    .orderByAsc(ZzCourse::getSortNo)
                    .orderByAsc(ZzCourse::getId));
        }
        Result<PageData<ApiZZCourseInfoVO>> result = new Result<>();
        PageData<ApiZZCourseInfoVO> pageData = new PageData<>();
        pageData.setTotal(page.getTotal());
        pageData.setRowData(convertApiZZCourseInfoVO(page.getRecords()));
        result.setData(pageData);
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        return result;
    }

    private List<ApiZZCourseInfoVO> convertApiZZCourseInfoVO(List<ZzCourse> zzCourseList) {
        List<ApiZZCourseInfoVO> apiZZCourseInfoVOList = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(zzCourseList)) {
            for (ZzCourse course : zzCourseList) {
                ApiZZCourseInfoVO apiZZCourseInfoVO = new ApiZZCourseInfoVO();
                BeanUtils.copyProperties(course, apiZZCourseInfoVO);
                apiZZCourseInfoVOList.add(apiZZCourseInfoVO);
            }
        }
        return apiZZCourseInfoVOList;
    }

    private List<ApiZzClassesInfoVO> convertApiZzClassesInfoVO(List<ZzClasses> zzClassesList) {
        List<ApiZzClassesInfoVO> apiZzClassesInfoVOS = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(zzClassesList)) {
            List<Map<String, Object>> result = zzZzStuClassesRService.countStudentsByClassIds(zzClassesList.stream().map(ZzClasses::getId).collect(Collectors.toList()));
            Map<Long, Long> dataMap = result.stream()
                    .collect(Collectors.toMap(
                            map -> MapUtils.getLong(map, "classId"),
                            map -> MapUtils.getLong(map, "count")
                    ));
            for (ZzClasses zzClasses : zzClassesList) {
                ApiZzClassesInfoVO apiZzClassesInfoVO = new ApiZzClassesInfoVO();
                BeanUtils.copyProperties(zzClasses, apiZzClassesInfoVO);
                apiZzClassesInfoVO.setStudentNum(MapUtils.getInteger(dataMap, zzClasses.getId(), 0));
                apiZzClassesInfoVOS.add(apiZzClassesInfoVO);
            }
        }
        return apiZzClassesInfoVOS;
    }
}
